/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.dao;


import com.aptech.beans.Customer;
import com.aptech.utility.DBType;
import com.aptech.utility.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author KG
 */
public class CustomerManager {
     public static ResultSet displayAllRows() throws SQLException {
        String sql = "SELECT * FROM Customers ";

        Connection conn = DBUtil.getConnection(DBType.myConnection);
        PreparedStatement preparedStatement = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
         public static  int insertCustomer(Customer  cus){
          
        int rs=0;
        String sql="insert into Customers values(?,?,?,?,?)";
        try {
            Connection conn = DBUtil.getConnection(DBType.myConnection);   
            PreparedStatement pre = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pre.setString(1,cus.getCustomerName());
            pre.setString(2,cus.getCustomerEmail());
            pre.setString(3,cus.getCustomerAddress());
            pre.setString(4,cus.getCustomerPhone());
            pre.setBoolean(5,cus.isIsRelationCustomer());          
            rs=pre.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }
          public static int updateCustomer(Customer cus) throws SQLException {

        String sql = "UPDATE Customers "
                + " SET CustomerName=?, CustomerEmail=?, CustomerAddress=?, CustomerPhone=?, IsRelationCustomer=?"
                + " WHERE CustomerID = ?";

        Connection conn = DBUtil.getConnection(DBType.myConnection);
        PreparedStatement pre = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
           
            pre.setString(1,cus.getCustomerName());
            pre.setString(2,cus.getCustomerEmail());
            pre.setString(3,cus.getCustomerAddress());
            pre.setString(4,cus.getCustomerPhone());
            pre.setBoolean(5,cus.isIsRelationCustomer());
            pre.setInt(6, cus.getCustomerID());
        int rs = pre.executeUpdate();
        return rs;

    }
    
    
}
